package org.inasnet.frame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.swing.*;

import org.inasnet.FontManager;
import org.inasnet.Root;
import org.inasnet.audio.AudioManager;
import org.inasnet.user.Session;
import org.inasnet.user.Session.TalkStatus;
import org.inasnet.util.Util;

public class TalkFrame extends JFrame
{
	public TalkFrame( Session session )
	{
		setTitle( Root.lang( "MAIN_Talk_Frame" ) );
		
		setLocation( 400, 500 );
		setSize( 300, 500 );
		
		//setAlwaysOnTop( true );  
		
		setResizable( false );
		
		setIconImage( Util.loadImage( "microphone_3.gif" ) );
		
		setLayout( new FlowLayout() );
		
		mVoicePanel = new VoicePanel();
		mVoicePanel.setSize( 80, 80 );
		mVoicePanel.setPreferredSize( new Dimension( 80, 80 ) );
		mVoicePanel.setBackground( Color.LIGHT_GRAY );
		getContentPane().add( mVoicePanel );
		
		mStatusLabel = new JLabel( "" );
		mStatusLabel.setSize( getWidth() - 100, 50 );
		mStatusLabel.setPreferredSize( new Dimension( getWidth() - 100, 50 ) );
		mStatusLabel.setFont( FontManager.getFont( "Times New Roman", 18 ) );
		
		getContentPane().add( mStatusLabel );
		
		updateStatus( TalkStatus.NONE );
		
		addWindowFocusListener( new FocusListener() ); 

	}
	
	class FocusListener implements WindowFocusListener
	{
		public void windowGainedFocus( WindowEvent e ) 
		{
			AudioManager.connect( mSession );
		}

		public void windowLostFocus( WindowEvent e ) 
		{
			
		}
	}
	
	public void updateStatus( TalkStatus status )
	{
		mStatus = status;
		
		switch( mStatus )
		{
		case NONE:
			mStatusLabel.setText( Root.lang( "TALK_No_Talk" ) );
			mStatusLabel.setForeground( Color.BLACK );
			break;
			
		case REQUEST:
			mStatusLabel.setText( Root.lang( "TALK_Request" ) );
			mStatusLabel.setForeground( Color.BLACK );
			break;
			
		case ONE_SIDE_DECLINED:
			mStatusLabel.setText( Root.lang( "TALK_Contact_Declined" ) );
			mStatusLabel.setForeground( Color.RED );
			break;
		}
	}
	
	public void dragOnTop() 
	{
		setFocusable( true );
	}
	
	class VoicePanel extends JPanel
	{
		
	}
	
	VoicePanel mVoicePanel;
	
	JLabel mStatusLabel;
	
	Session.TalkStatus mStatus;
		
	Session mSession;
}
